/**
 * 通用混合
 **/
// 配置
import Setting from '@/setting.js';
import { frameInRoutes } from '@/router/routes';
// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu } from '@/libs/system';
export default {
    data() {
        return {
            menuSider: null,
            menuHeader: null,
            userInfo: null
        }
    },
    methods: {
        // 当 $route 更新时触发
        appRouteChange(to, from) {
            if (to.matched.length == 0) {
                this.$router.replace('/');
                return
            }
            let path = to.matched[to.matched.length - 1].path;
            if (!Setting.dynamicSiderMenu) {
                let headerName = getHeaderName(path, this.menuSider)
                if (headerName === null) {
                    path = to.path;
                    headerName = getHeaderName(path, this.menuSider);
                }
                if (headerName !== null) {
                    this.$store.commit('admin/menu/setHeaderName', headerName);
                    // 查询侧边栏菜单，并设置渲染
                    const filterMenuSider = getMenuSider(this.menuSider, headerName);
                    this.$store.commit('admin/menu/setSider', filterMenuSider);
                    // 高亮子菜单
                    this.$store.commit('admin/menu/setActivePath', to.path);
                    // 打开默认选中菜单
                    const openNames = getSiderSubmenu(path, this.menuSider);
                    this.$store.commit('admin/menu/setOpenNames', openNames);
                }
            }
        },
        initApp() {
            // 处理路由 得到每一级的路由设置
            this.$store.commit('admin/page/init', frameInRoutes);
            // 初始化全屏监听
            this.$store.dispatch('admin/layout/listenFullscreen')
        },
        initNavigation() {
            this.$store.commit('admin/menu/setHeader', this.menuHeader);
            this.$store.commit('admin/menu/setMenuSider', this.menuSider);
            this.$store.dispatch('admin/user/set', this.userInfo, { root: true });
        },
        setCache() {
            this.menuHeader = icu.storage.getData('menuHeader');
            this.menuSider = icu.storage.getData('menuSider');
            this.userInfo = icu.storage.getData('userInfo');
        },
    }
}